package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.dto.ProTypeDto;
import com.example.mapper.ProTypeMapper;
import com.example.service.ProTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class ProTypeServiceImpl implements ProTypeService {
    @Autowired
    private ProTypeMapper mapper;

    @Override
    public void add(ProTypeDto dto) {
        mapper.insert(dto);
    }

    @Override
    public IPage<ProTypeDto> select(ProTypeDto dto) {
        QueryWrapper<ProTypeDto> queryWrapper = new QueryWrapper<>();
        if (dto.getCategoryName()!=""&&dto.getCategoryName()!=null){
            queryWrapper.eq("category_name",dto.getCategoryName());
        }
        Page<ProTypeDto> page = new Page<>(dto.getPageNum(), dto.getPageSize());
        IPage<ProTypeDto> result = mapper.selectPage(page,queryWrapper);
        return result;
    }

    @Override
    public List<Map<String, Object>> selectName() {
       return mapper.selectName();
    }

    @Override
    public void update(ProTypeDto dto) {
        QueryWrapper<ProTypeDto> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",dto.getId());
        mapper.update(dto,queryWrapper);
    }

    @Override
    public void delete(ProTypeDto dto) {
        QueryWrapper<ProTypeDto> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",dto.getId());
        mapper.delete(queryWrapper);
    }
}
